package com.hzlx.component.controller;

import java.util.List;
import java.io.IOException;

import com.hzlx.BomComponentsDto;
import com.hzlx.BomsDto;
import com.hzlx.boms.domain.Boms;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.component.domain.BomComponents;
import com.hzlx.component.service.IBomComponentsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * BOM组件明细Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/base")
public class BomComponentsController extends BaseController
{
    @Autowired
    private IBomComponentsService bomComponentsService;

    /**
     * 查询BOM组件明细列表
     */
    @RequiresPermissions("base-data-service:components:list")
    @GetMapping("/components/list")
    public TableDataInfo list(BomComponents bomComponents)
    {
        startPage();
        List<BomComponents> list = bomComponentsService.selectBomComponentsList(bomComponents);
        return getDataTable(list);
    }

    @GetMapping("/components/operations/list")
    public TableDataInfo OPlist(BomComponents bomComponents)
    {
        startPage();
        List<BomComponents> oplist = bomComponentsService.selectBomComponentsOperationsList(bomComponents);
        return getDataTable(oplist);
    }

    @GetMapping("/componentsDto/list")
    public List<BomComponentsDto> listDto(BomComponentsDto bomComponentsDto)
    {

       BomComponents bomComponents=new BomComponents();
        // 复制属性
        BeanUtils.copyProperties(bomComponentsDto, bomComponents);
        startPage();
        List<BomComponents> list = bomComponentsService.selectBomComponentsList(bomComponents);
        List<BomComponentsDto> dtoList = list.stream()
                .map(entity -> {
                    BomComponentsDto dto = new BomComponentsDto();
                    dto.setComponentMaterialId(entity.getComponentMaterialId());
                    dto.setQuantity(entity.getQuantity());
                    dto.setUnitOfMeasure(entity.getUnitOfMeasure());
                    // 其他属性...
                    return dto;
                })
                .toList();
        startPage();
        return dtoList;
    }
    /**
     * 导出BOM组件明细列表
     */
    @RequiresPermissions("base-data-service:components:export")
    @Log(title = "BOM组件明细", businessType = BusinessType.EXPORT)
    @PostMapping("/components/export")
    public void export(HttpServletResponse response, BomComponents bomComponents)
    {
        List<BomComponents> list = bomComponentsService.selectBomComponentsList(bomComponents);
        ExcelUtil<BomComponents> util = new ExcelUtil<BomComponents>(BomComponents.class);
        util.exportExcel(response, list, "BOM组件明细数据");
    }

    /**
     * 获取BOM组件明细详细信息
     */
    @RequiresPermissions("base-data-service:components:query")
    @GetMapping(value = "/components/{componentId}")
    public AjaxResult getInfo(@PathVariable("componentId") Long componentId)
    {
        return success(bomComponentsService.selectBomComponentsByComponentId(componentId));
    }

    /**
     * 新增BOM组件明细
     */
    @RequiresPermissions("base-data-service:components:add")
    @Log(title = "BOM组件明细", businessType = BusinessType.INSERT)
    @PostMapping("/components")
    public AjaxResult add(@RequestBody BomComponents bomComponents)
    {
        return toAjax(bomComponentsService.insertBomComponents(bomComponents));
    }

    /**
     * 修改BOM组件明细
     */
    @RequiresPermissions("base-data-service:components:edit")
    @Log(title = "BOM组件明细", businessType = BusinessType.UPDATE)
    @PutMapping("/components")
    public AjaxResult edit(@RequestBody BomComponents bomComponents)
    {
        return toAjax(bomComponentsService.updateBomComponents(bomComponents));
    }

    /**
     * 删除BOM组件明细
     */
    @RequiresPermissions("base-data-service:components:remove")
    @Log(title = "BOM组件明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/components/{componentIds}")
    public AjaxResult remove(@PathVariable Long[] componentIds)
    {
        return toAjax(bomComponentsService.deleteBomComponentsByComponentIds(componentIds));
    }

    @GetMapping("/components/materials")
    public List<BomComponents> getMaterials(Long bomId, Long operationId){
        return bomComponentsService.selectMaterials(bomId,operationId);
    }
}
